package com.zhongkun.datahome.service.res.house;

import com.zhongkun.datahome.core.domain.vo.UserBaseVo;
import com.zhongkun.datahome.service.constant.YesOrNoEnum;
import com.zhongkun.datahome.service.domain.CustomerBase;
import com.zhongkun.datahome.service.domain.CustomerHouse;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/16 14:49
 * @Desc
 */
@Data
public class CustomerHouseDetailVo {
    @ApiModelProperty("房产信息")
    private CustomerHouse customerHouse;

    @ApiModelProperty("业主信息")
    private CustomerBase customerBase;

    @ApiModelProperty("服务团队")
    private List<ServerUser> serverUserList;

    @ApiModelProperty("家庭成员")
    private List<FamilyMember> familyMemberList;

    @Data
    public static class ServerUser {
        @ApiModelProperty("服务人员商户userId")
        private Long tenantUserId;

        @ApiModelProperty("服务人员用户名")
        private String username;

        @ApiModelProperty("服务人员姓名")
        private String nickName;

        @ApiModelProperty("服务人员头像")
        private String avatar;

        @ApiModelProperty("服务人员手机")
        private String mobile;

        @ApiModelProperty("服务人员职位")
        private String post;

        public static ServerUser of(UserBaseVo userBaseVo) {
            ServerUser serverUser = new ServerUser();
            // serverUser.setUserId(userBaseVo.getUserId());
            serverUser.setTenantUserId(userBaseVo.getTenantUserId());
            serverUser.setUsername(userBaseVo.getUserName());
            serverUser.setNickName(userBaseVo.getNickName());
            serverUser.setAvatar(userBaseVo.getAvatar());
            serverUser.setMobile(userBaseVo.getPhonenumber());
            serverUser.setPost(userBaseVo.getPostName());
            return serverUser;
        }
    }

    @Data
    public static class FamilyMember {
        @ApiModelProperty("客户id")
        private Long customerId;

        @ApiModelProperty("客户姓名")
        private String customerName;

        @ApiModelProperty("客户手机")
        private String mobile;

        @ApiModelProperty("与户主关系")
        private String relation;

        public static FamilyMember of(CustomerBase customerBase) {
            FamilyMember member = new FamilyMember();
            member.setCustomerId(customerBase.getId());
            member.setCustomerName(customerBase.getCustomerName());
            member.setMobile(customerBase.getMobile());
            member.setRelation(customerBase.getRelation());
            return member;
        }
    }

    public static CustomerHouseDetailVo of(CustomerHouse customerHouse, List<CustomerBase> memberInfo,
                                           List<UserBaseVo> serverUserBaseVos) {
        CustomerHouseDetailVo customerHouseDetailVo = new CustomerHouseDetailVo();
        // 房产信息
        customerHouseDetailVo.setCustomerHouse(customerHouse);

        // 业主信息
        Optional<CustomerBase> ownerInfo = memberInfo.stream()
                .filter(item -> YesOrNoEnum.YES.is(item.getIsOwner()))
                .findFirst();
        customerHouseDetailVo.setCustomerBase(ownerInfo.orElse(null));

        // 家庭成员
        List<FamilyMember> otherMemberInfo = memberInfo.stream()
                .filter(item -> !YesOrNoEnum.YES.is(item.getIsOwner()))
                .map(FamilyMember::of)
                .collect(Collectors.toList());
        customerHouseDetailVo.setFamilyMemberList(otherMemberInfo);

        // 服务团队
        List<ServerUser> serverUsers = serverUserBaseVos.stream()
                .map(ServerUser::of)
                .collect(Collectors.toList());
        customerHouseDetailVo.setServerUserList(serverUsers);
        return customerHouseDetailVo;
    }
}
